﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Beway.ValueAdded.Service
{
    public class BatchDetailSvc
    {
        /// <summary>
        /// 批量增加充值明细信息,并增加批次信息
        /// </summary>
        /// <param name="list">充值明细</param>
        /// <returns>新增成功的数量</returns>
        public int BatchAdd(List<BatchDetail> list, BatchRecharge br)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {    //保存批次信息
                db.BatchRecharge.Add(br);
                int ret = db.SaveChanges();

                list.ForEach(m => m.BatchId = br.Id);//更新明细详细的批次id

                if (db.Database.Connection.State != ConnectionState.Open)
                {
                    db.Database.Connection.Open(); //打开Connection连接
                }

                //调用BulkInsert方法,将list集合数据批量插入到数据库的BatchDetail表中
                ServiceUtils.BulkInsert<BatchDetail>((SqlConnection)db.Database.Connection, "BatchDetail", list);
 
                if (db.Database.Connection.State != ConnectionState.Closed)
                {
                    db.Database.Connection.Close(); //关闭Connection连接
                }
              

                return ret;                
            }
        }

        /// <summary>
        /// 分页的批量充值详细数据
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="rows">每页数量</param>
        /// <param name="reUserId">项目id</param>
        /// <param name="batchId">批量id</param>
        /// <param name="uploadUserId">上传用户id</param>
        /// <param name="adminId">项目管理员id</param>
        /// <param name="ListCount">返回总数量</param>
        /// <returns></returns>
        public List<BatchDetailView> List(int page, int rows, int reUserId, string batchName, string mobileNum, int rechStatus, int uploadUserId, int adminId, out int ListCount)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                IQueryable<BatchDetailView> q = from r in db.BatchDetail
                                                join b in db.BatchRecharge on r.BatchId equals b.Id
                                                join u in db.AdminUser on b.UploadUserId equals u.AdminID
                                                join c in db.AdminUser on b.CheckUserId equals c.AdminID into checkTmp
                                                from check in checkTmp.DefaultIfEmpty()
                                                join p in db.ReUser on b.ReUserId equals p.UserID
                                                select new BatchDetailView
                                                {
                                                    Id = r.Id,
                                                    AddTime = r.AddTime,
                                                    BatchId = r.BatchId,
                                                    CheckUser = check.AdminUserName + "(" + u.TrueName + ")",
                                                    BatchName = b.BatchName,
                                                    MobileNum = r.MobileNum,
                                                    ProjectName = p.UserTitle,
                                                    RechMsg = r.RechMsg,
                                                    RechStatus = r.RechStatus,
                                                    RechTime = r.RechTime,
                                                    RechValue = r.RechValue,
                                                    UploadUser = u.AdminUserName + "(" + u.TrueName + ")",
                                                    UploadUserId = u.AdminID,
                                                    AdminId = p.AdminId ?? 0,
                                                    ProjectId = p.UserID
                                                };


                if (!String.IsNullOrEmpty(batchName))
                {
                    q = q.Where(m => m.BatchName == batchName);
                }

                if (!String.IsNullOrEmpty(mobileNum))
                {
                    q = q.Where(m => m.MobileNum == mobileNum);
                }
                if (rechStatus > -1)
                {
                    q = q.Where(m => m.RechStatus == rechStatus);
                }

                if (uploadUserId > 0)
                {
                    q = q.Where(m => m.UploadUserId == uploadUserId);
                }
                if (reUserId > 0)
                {
                    q = q.Where(m => m.ProjectId == reUserId);
                }
                if (adminId > 0)
                {
                    q = q.Where(m => m.AdminId == adminId);
                }

                q = q.OrderByDescending(m => m.Id);

                ListCount = q.Count();

                var adm = q.Skip((page - 1) * rows).Take(rows).ToList();
                return adm;
            }
        }

        /// <summary>
        /// 通过batchId获取所有记录
        /// </summary>
        /// <param name="batchId"></param>
        /// <returns></returns>
        public List<BatchDetail> GetListByBatchId(int batchId)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var list = db.BatchDetail.Where(m => m.BatchId == batchId);
                return list.ToList();
            }
        }

        /// <summary>
        /// 更新批量充值明细信息，只更新了rechstatus,rechmsg,rechtime信息
        /// </summary>
        /// <param name="bd"></param>
        /// <returns></returns>
        public bool Update(BatchDetail bd)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var m = db.BatchDetail.Find(bd.Id);
                if (m != null)
                {
                    m.RechStatus = bd.RechStatus;
                    m.RechMsg = bd.RechMsg;
                    m.RechTime = bd.RechTime;
                    return db.SaveChanges() > 0;
                }
            }
            return false;
        }
    }
}
